package com.cljz.makepassenger.present.activity;

import com.cljz.makepassenger.base.App;
import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.base.Constant;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.SetUserInfoBean;
import com.cljz.makepassenger.model.net.bean.UploadImgBean;
import com.cljz.makepassenger.ui.activity.UserLogoActivity;
import com.cljz.makepassenger.utils.L;
import com.clzj.makepassenger.bean.UserInfo;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.droidlover.xdroidmvp.cache.SharedPref;
import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

/**
 * Created by apple on 2017/4/12.
 */

public class UserLogoP extends BaseP<UserLogoActivity> {
    public void getUploadImg(String img) {
        JSONObject jsonObject = new JSONObject();
        JSONArray array = new JSONArray();
        JSONObject inner = new JSONObject();
        try {
            inner.put("content", img);
            inner.put("ext", "jpg");
            array.put(inner);
            jsonObject.put("token", getToken());
            jsonObject.put("type", 3);
            jsonObject.put("file", array);
            String body = Base64Utils.enCode(jsonObject.toString());
            RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
            Api.getResponseInfoAPI().getUpLoadImgData(requestBody)
                    .compose(XApi.<UploadImgBean>getApiTransformer())         //异常处理
                    .compose(XApi.<UploadImgBean>getScheduler())              //线程切换
                    .compose(getV().<UploadImgBean>bindToLifecycle())
                    .subscribe(new ApiSubcriber<UploadImgBean>() {
                        @Override
                        protected void onFail(NetError error) {
                            L.e(error.toString());
                        }

                        @Override
                        public void onNext(UploadImgBean uploadImgBean) {
                            if ("0".equals(uploadImgBean.getError_code())) {
                                setUserLogo(uploadImgBean.getData());
                            } else if ("1000".equals(uploadImgBean.getError_code())) {
                                getV().loginOvertime(1);
                            } else {
                                getV().getUpLoadImgFail(uploadImgBean.getMsg());
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setUserLogo(UploadImgBean.DataBean data) {
        final String url = data.getDetail().get(0).getUrl();
        Map<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("user_logo", url);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().setUserInfoData(requestBody)
                .compose(XApi.<SetUserInfoBean>getApiTransformer())         //异常处理
                .compose(XApi.<SetUserInfoBean>getScheduler())              //线程切换
                .compose(getV().<SetUserInfoBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<SetUserInfoBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(SetUserInfoBean setUserInfoBean) {
                        if ("0".equals(setUserInfoBean.getError_code())) {
                            initDB(setUserInfoBean.getData().getNewX(), url);
                            getV().userLogoUpdate(setUserInfoBean.getMsg());
                        } else if ("1000".equals(setUserInfoBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getUpLoadImgFail(setUserInfoBean.getMsg());
                        }
                    }
                });
    }

    private void initDB(String newToken, String url) {
        UserInfo userInfo = getLocalUserInfo(getToken());
        SharedPref.getInstance(App.getInstance()).putString(Constant.TOKEN, newToken);
        daoSession.getUserInfoDao().deleteAll();
        userInfo.setToken(newToken);
        userInfo.setUser_logo(url);
        daoSession.getUserInfoDao().insert(userInfo);
    }

}
